#!/usr/bin/python3
# -*- coding: utf-8 -*-

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os

observer = Observer()
target_path = r''
sys_name = ''
videos = ['MPG', 'MP4', 'MOV', 'WMV', 'AVI']
photos = ['JPEG', 'TIFF', 'BMP', 'GIF', 'PNG', 'JPG']
audios = ['WAV', 'MP3', 'OGG']
documents = ['DOCX', 'ODF', 'DOC', 'TXT', 'PDF']
photoshop = ['PSD']
archives = ['7Z', 'RAR', 'ZIP', 'TAR', 'TAR-GZ', 'TGZ', 'ZIPX']
executables = ['EXE', 'PY', 'PYX', 'PYC', 'PYW', 'SH', 'APK', 'JAR', 'APPIMAGE']
torrent = ['TORRENT']
iso = ['ISO', 'IMG', 'CSO', 'DMG', 'IMA']
source_code = ['C', 'CPP', 'CS']


if 'Windows' in sys_name:
    from ctypes import windll
else:
    pass


def if_exists(path, file_type, file):
    index = 0
    new_file_name = file
    if file_type in ['Audio', 'Video', 'Photos']:
        file_type = os.path.join('Media', file_type)

    check_path = os.path.join(path, file_type, new_file_name)

    while True:
        index += 1
        if os.path.exists(check_path):
            new_file_name = new_file_name.split('.')
            new_file_name.insert(-1, f'_{str(index)}')
            new_file_name.insert(-1, '.')
            new_file_name = ''.join(new_file_name)
            check_path = os.path.join(path, file_type, new_file_name)
        else:
            return check_path


def sorter(filename, paths, file):
    if 'Windows' in sys_name:
        if windll.user32.OpenClipboard(None):
            windll.user32.EmptyClipboard()
            windll.user32.CloseClipboard()
    else:
        pass
    if filename[-1][-1].upper() in videos:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Video', file))
    elif filename[-1][-1].upper() in audios:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Audio', file))
    elif filename[-1][-1].upper() in documents:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Documents', file))
    elif filename[-1][-1].upper() in photos:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Photos', file))
    elif filename[-1][-1].upper() in photoshop:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Photoshop', file))
    elif filename[-1][-1].upper() in archives:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Archives', file))
    elif filename[-1][-1].upper() in executables:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Executable', file))
    elif filename[-1][-1].upper() in torrent:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Torrents', file))
    elif filename[-1][-1].upper() in iso:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Disks', file))
    elif filename[-1][-1].upper() in source_code:
        os.renames(os.path.join(paths, file), if_exists(target_path, 'Code', file))


class MyWatchdog(FileSystemEventHandler):
    def on_modified(self, event):
        for paths, dirs, files in os.walk(target_path):
            if paths != target_path:
                break
            for file in files:
                filename = list()
                filename.append(file.split('.'))
                sorter(filename, paths, file)


if __name__ == '__main__':
    event_handler = MyWatchdog()
    observer.schedule(event_handler, target_path)
    observer.start()
    try:
        while observer.is_alive():
            observer.join(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
